<template>
    <div>
        <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>产品质量管理</el-breadcrumb-item>
        <el-breadcrumb-item>不合格代码维护</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <el-row :gutter="10">
            <el-col :span="7">
                <el-input placeholder="请输入不良组编号检索" v-model="query">
                    <template #append>
                        <el-button icon="el-icon-search" @click="getList">检索</el-button>
                    </template>
                </el-input>
            </el-col>
            <el-col span="4">
                <el-button type="primary" icon="el-icon-plus"  @click="addDialogVisible = true">新增</el-button>
            </el-col>
        </el-row>
        <!-- 表格区域 -->
        <el-table stripe border
        ref="multipleTable"
        :data="ncList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
    <el-table-column prop="id" label="id" width="220" align="center">
    </el-table-column>
    <el-table-column prop="nc_code" label="不良代码编号" width="220" align="center">
    </el-table-column>
    <el-table-column prop="nc_code_desc" label="不良代码描述" width="220" align="center">
    </el-table-column>
    <el-table-column prop="nc_code_group_no" label="所属不良代码组" width="220" align="center">
    </el-table-column>
    <el-table-column prop="site" label="挂靠站点" width="240" align="center">
    </el-table-column>
    <el-table-column prop="by_user" label="创建人" width="240" align="center">
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间" width="220" align="center">
      <template #default="scope">
            {{formatDate(scope.row.create_time)}}
      </template>
    </el-table-column>
    <el-table-column label="操作" width="120" align="center" fixed="right">
      <template #default="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页区域 -->
   <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNow"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="rowCount">
    </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="新增不良代码" v-model="addDialogVisible" width="40%">
      <!-- 新增不合格组对话框 -->
    <el-dialog title="不合格组列表" v-model="groupDV" width="40%" append-to-body>
    <!-- 对话框内容 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="groupList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        size="small"
      >
        <el-table-column prop="id" label="id" width="250" align="center">
    </el-table-column>
    <el-table-column prop="nc_code_group_no" label="不良代码组编号" width="250" align="center">
    </el-table-column>
    <el-table-column prop="nc_code_group_desc" label="不良代码组描述" width="250" align="center">
    </el-table-column>
    <el-table-column prop="site" label="挂靠站点" width="250" align="center">
    </el-table-column>
    <el-table-column prop="by_user" label="创建人" width="250" align="center">
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间" width="250" align="center">
      <template #default="scope">
            {{formatDate(scope.row.create_time)}}
      </template>
    </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              @click="addGroup(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="groupDV = false">取 消</el-button>
        </span>
      </template>
    </el-dialog>
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormrules"
        ref="addFormRef"
        label-width="135px"
      >
        <el-form-item label="所属站点" prop="site">
          <el-input v-model="addForm.site" disabled></el-input>
        </el-form-item>
        <el-form-item label="不合格代码编号" prop="nc_code">
          <el-input v-model="addForm.nc_code" placeholder="请输入不良代码编号"></el-input>
        </el-form-item>
        <el-form-item label="不合格代码描述" prop="nc_code_desc">
          <el-input v-model="addForm.nc_code_desc" placeholder="请输入不良代码描述"></el-input>
        </el-form-item>
        <el-form-item label="所属不良代码组" prop="nc_code_group_no">
          <el-input v-model="addForm.nc_code_group_no" placeholder="请选择所属不良代码组"> 
                <template #suffix>
                  <i
                    class="el-input__icon el-icon-search"
                    @click="addgroup"
                  ></i>
                </template>
              </el-input>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="addForm.state"
            placeholder="请选择是否可用"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑不良代码" v-model="editDialogVisible" width="40%">
      <!-- 不合格组对话框 -->
    <el-dialog title="不合格组列表" v-model="groupDV" width="40%" append-to-body>
    <!-- 对话框内容 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="groupList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        size="small"
      >
        <el-table-column prop="id" label="id" width="250" align="center">
    </el-table-column>
    <el-table-column prop="nc_code_group_no" label="不良代码组编号" width="250" align="center">
    </el-table-column>
    <el-table-column prop="nc_code_group_desc" label="不良代码组描述" width="250" align="center">
    </el-table-column>
    <el-table-column prop="site" label="挂靠站点" width="250" align="center">
    </el-table-column>
    <el-table-column prop="by_user" label="创建人" width="250" align="center">
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间" width="250" align="center">
      <template #default="scope">
            {{formatDate(scope.row.create_time)}}
      </template>
    </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              @click="editGroup(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="groupDV = false">取 消</el-button>
        </span>
      </template>
    </el-dialog>
      <!-- 对话框内容 -->
      <el-form
        :model="editForm"
        status-icon
        :rules="addFormrules"
        ref="editFormRef"
        label-width="135px"
      >
        <el-form-item label="所属站点" prop="site">
          <el-input v-model="editForm.site" disabled></el-input>
        </el-form-item>
        <el-form-item label="不合格代码编号" prop="nc_code">
          <el-input v-model="editForm.nc_code" placeholder="请输入不良代码编号"></el-input>
        </el-form-item>
        <el-form-item label="不合格代码描述" prop="nc_code_desc">
          <el-input v-model="editForm.nc_code_desc" placeholder="请输入不良代码描述"></el-input>
        </el-form-item>
        <el-form-item label="所属不良代码组" prop="nc_code_group_no">
          <el-input v-model="editForm.nc_code_group_no" placeholder="请输入选择不良代码组">
            <template #suffix>
                  <i
                    class="el-input__icon el-icon-search"
                    @click="editgroup"
                  ></i>
                </template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="editForm.state"
            placeholder="请选择是否可用"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      ncList:[],
      pageCount:1,
      pageNow:1,
      pageSize:10,
      rowCount:0,
      query:"",
      // 显示添加对话框
      addDialogVisible: false,
      // 显示编辑对话框
      editDialogVisible: false,
      groupDV:false,
      groupList:[],
      addForm: {
        site: "*",
        nc_code: "",
        nc_code_desc: "",
        nc_code_group_no:"",
        state: "",
      },
      editForm: {},
      addFormrules: {
        site: [],
        nc_code: [
          {
            required: true,
            message: "请输入不良代码编号",
            trigger: "blur",
          },
        ],
        nc_code_desc: [
          {
            required: true,
            message: "请输入不良代码描述",
            trigger: "blur",
          },
        ],
        nc_code_group_no: [
          {
            required: true,
            message: "请输入不良代码组",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择是否可用",
            trigger: "blur",
          },
        ],
      },
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList(){
      this.$axios({
        headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
        method: "post",
        url: "/IE_MES/qc/nc_findByPage.shtml",
        data: this.$qs.stringify({
              "pageSize":this.pageSize,
              "pageNow": this.pageNow,
              "ncCodeFormMap.nc_code": this.query
            }),
      })
        .then((res) => {
        this.ncList = res.data.records
        this.pageCount = res.data.pageCount
        this.pageNow = res.data.pageNow
        this.pageSize = res.data.pageSize
        this.rowCount = res.data.rowCount
        });
    },
    // 新增
    add() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/qc/nc_addEntity.shtml",
        data: this.$qs.stringify({
          "ncCodeFormMap.site": this.addForm.site,
          "ncCodeFormMap.nc_code":this.addForm.nc_code,
          "ncCodeFormMap.nc_code_desc":this.addForm.nc_code_desc,
          "ncCodeGroupFormMap.nc_code_group_no": this.addForm.nc_code_group_no,
          "ncCodeFormMap.state": this.addForm.state,
        }),
      }).then((res) => {
        if (res.data == '{"result":"success","score":""}') {
          this.getList();
          this.$refs['addFormRef'].resetFields();
          this.addDialogVisible = false;
          this.$message({
            type: "success",
            message: "添加成功!",
          });
        }
      });
    },
    addgroup(){
      this.groupDV = true;
        this.$axios({
        headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
        method: "post",
        url: "/IE_MES/nc_group/nc_group_findByPage.shtml",
        data: this.$qs.stringify({
              "pageSize":this.pageSize,
              "pageNow": this.pageNow,
            }),
      })
        .then((res) => {
          this.groupList = res.data.records;
          this.pageCount = res.data.pageCount;
          this.pageNow = res.data.pageNow;
          this.pageSize = res.data.pageSize;
          this.rowCount = res.data.rowCount;
        });
    },
    addGroup(row) {
      this.addForm.nc_code_group_no = row.nc_code_group_no;
      this.groupDV = false;
    },
    // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      this.editDialogVisible = true;
    },
    editgroup(){
      this.groupDV = true;
        this.$axios({
        headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
        method: "post",
        url: "/IE_MES/nc_group/nc_group_findByPage.shtml",
        data: this.$qs.stringify({
              "pageSize":this.pageSize,
              "pageNow": this.pageNow,
            }),
      })
        .then((res) => {
          this.groupList = res.data.records;
          this.pageCount = res.data.pageCount;
          this.pageNow = res.data.pageNow;
          this.pageSize = res.data.pageSize;
          this.rowCount = res.data.rowCount;
        });
    },
    editGroup(row) {
      this.editForm.nc_code_group_no = row.nc_code_group_no;
      this.groupDV = false;
    },
    edit() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/qc/nc_editEntity.shtml",
        data: this.$qs.stringify({
          "ncCodeFormMap.id": this.editForm.id,
          "ncCodeFormMap.site": this.editForm.site,
          "ncCodeFormMap.nc_code":this.editForm.nc_code,
          "ncCodeFormMap.nc_code_desc":this.editForm.nc_code_desc,
          "ncCodeFormMap.nc_code_group_no": this.editForm.nc_code_group_no,
          "ncCodeFormMap.state": this.editForm.state,
        }),
      }).then((res) => {
        if (res.data == "success") {
          this.getList();
          this.editDialogVisible = false;
          this.$message({
            type: "success",
            message: "编辑成功!",
          });
        }
      });
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该资源, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/qc/nc_deleteEntity.shtml?_t="+new Date(),
            data: this.$qs.stringify({
              ids:row.id
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange (newSize) {
      this.pageSize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.pageNow = newPage
      this.getList()
    },
    formatDate(data) {
        var date = new Date(data);
        var YY = date.getFullYear() + '-';
        var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
        var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return YY + MM + DD + " " + hh + mm + ss;
    },
  }
}
</script>

<style lang="less" scoped>
.el-form-item /deep/ .el-input__inner {
  padding-right: 420px !important;
}
</style>
